package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.panels.images.ImageWithLink;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.FolderURLPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.dao.FilesUploadedDAO;
import com.edushare.common.util.DateTimeUtil;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class FileUploadMessagePanel extends Panel {

	private CayenneModel<? extends IFileUploadable> senderModel;
	private CayenneModel<FileDirectories> dir;
	private CayenneModel<FilesUploaded> file;
	private CayenneModel<Users> uploader;
	private boolean isVisible = true;
	
	public FileUploadMessagePanel(String id, IModel<Messages> model) {
		super(id, model);
		getDataFromMessage(model);
		if(isVisible){
			add(new ImageWithLink("senderImage", senderModel, ApplicationConstants.FILE_SUFFIX_ICON));
			add(new EduShareURLPanel("name", senderModel));
			add(new FolderURLPanel("dirName", dir));
			add(new ExternalLink("fileLink", file.getObject().getFileDownloadUrl(), file.getObject().getFileOrigName()));
			add(new EduShareURLPanel("sender", uploader));
			add(new Label("date", DateTimeUtil.formatTimestamp(file.getObject().getUploadTimestamp())));
		}
	}

	private void getDataFromMessage(IModel<Messages> model) 
	{
		String msg = model.getObject().getMessage();
		String eType= null;
		String fid = null;
		
		if(StringUtil.isValid(msg)){
			//markup "<<<EDUSHAREMESSAGINGMETADATA:FILEUPLOAD:"+ eventType + ":" + fileUploaded.getFileUploadedId() + ":>>>"
			String[] parts = msg.split(":");
			if(parts!=null && parts.length>3){
				eType = parts[2];
				fid = parts[3];
			}
			
			if(fid!=null){
				FilesUploaded fileObj = new FilesUploadedDAO(EduShareSession.get().getSessionORMInterface())
											.getFileById(Integer.parseInt(fid));
				if(fileObj!=null){
					this.file = new CayenneModel<FilesUploaded>(fileObj);
					this.dir = new CayenneModel<FileDirectories>(fileObj.getFileDirectory());
					this.senderModel = new CayenneModel<IFileUploadable>
										(new FileDirectoriesDAO(EduShareSession.get().getSessionORMInterface())
										.getOwnerObject(fileObj.getFileDirectory()));
					this.uploader = new CayenneModel<Users>(fileObj.getUploader());
				}else{
					this.isVisible = false;
				}
			}
		}
	}
	
	@Override
	protected void onBeforeRender() {
		if(!isVisible){
			this.replaceWith(new EmptyPanel(getId()));
			this.setVisible(false);
		}
		super.onBeforeRender();
	}

	public static boolean exists(String msgKey) {
		String fid = null;
		//markup "<<<EDUSHAREMESSAGINGMETADATA:FILEUPLOAD:"+ eventType + ":" + fileUploaded.getFileUploadedId() + ":>>>"
		String[] parts = msgKey.split(":");
		if(parts!=null && parts.length>3){
			fid = parts[3];
		}
		
		if(	fid==null ||
			new FilesUploadedDAO(EduShareSession.get().getSessionORMInterface())
				.getFileById(Integer.parseInt(fid))==null){
			return false;
		}
		return true;
	}

}
